<template>
    <div class="tabs">
        <el-tag
            v-for="(item,index) in tags"
            :key="item.path"
            :closable="item.name !== 'home'"
            :effect="$route.name === item.name ? 'dark' : 'plain'"
            @click="changeMenu(item)"
            @close="handleClose(item,index)"
            size="small"
            >
            {{ item.label }}
        </el-tag>
    </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
export default {
    data() {
        return {

        }
    },
    computed: {
        ...mapState({
            tags: state => state.tab.tabsList
        })
        
    },
    methods: {
        ...mapMutations('tab', ['closeTag']),
        changeMenu(item) {
            // if ((item.path === '/' || item.path === '/home') && (this.$route.path === '/' || this.$route.path === '/home')) {
            //     return
            // }
            if (this.$route.path !== item.path) {
                this.$router.push({name: item.name})
            }
        },
        handleClose(item) {
            //this.$store.commit('tab/closeTag', item)
            this.closeTag(item)
            // 删除后，路由跳转到最后一个tag的路由
            if (this.$route.name === item.name) {
                const lastIndex = this.tags.length - 1
                this.$router.push(this.tags[lastIndex].path)
            }
        }
    }
}
</script>

<style lang="less" scoped>
.tabs {
  background-color: rgb(244,244,244);
  padding: 5px;
  .el-tag {
      //margin-right: 10px;
      margin-left: 10px;
      cursor: pointer;
  }
}
</style>